#include "compat.h"
#include "utypes.h"
#include "menu.h"
#include "mouse.h"

#ifdef EMBED
void setup_ints()
{
	SetISR(IRQ2INT(T0_IRQ), asm_wait);
	enable_irq(T0_IRQ);
	STI;
}

void leave()
{
	printString("This game only works in graphic mode.");
	while( hasKey() ) getKey();
	char a;
	while( ( a = getKey() ) != '\r') ;

	outportb(0x64, 0xFE);
}
#endif

void setup_peripherals()
{
	timer_init(0,LCOM_MODE);
	timer_load(0, get_divider(1000.0));
	timer_init(2,LCOM_MODE);
}


extern uchar mouse[3];
extern int num_byte;
int pack_completo;

#ifdef EMBED
void main(long vmem)
#else


int scancode;


int main( int argc , char * argv[] )
#endif
{
	char* video_base;
	Word base;
	char table[256 * CHR_H];
	base = COM1_ADD;
	int mouse_sup = 0;
#ifdef EMBED
    int scancode = 0;
	setup_ints();
	while(hasKey()) getKey();
	if ( vmem != 0 )
		video_base = (char*)vmem;
	else
		leave();
	srand(msec);
#else
	void kbd_isr(void);
	void mouse_isr(void);
	void asm_wait(void);
	srand(time(NULL));
	if ( argc > 1)
	{
		switch( atoi(argv[1]) )
		{
			case 2: base = COM2_ADD;
					break;
		}
	}
	disable_irq(KBD_IRQ);
	disable_irq(MOUSE_IRQ);
	disable_irq(T0_IRQ);
	   _go32_dpmi_seginfo old_kbd_irq , old_timer_irq , old_mouse_irq;
	install_asm_irq_handler(KBD_IRQ, kbd_isr, &old_kbd_irq);
	install_asm_irq_handler(MOUSE_IRQ, mouse_isr, &old_mouse_irq);
	install_asm_irq_handler(T0_IRQ, asm_wait, &old_timer_irq);
	__dpmi_meminfo map;
	/*graphic mode 105 (1024x768@8bits)*/
	video_base = enter_graphics(0x105, &map);
	
	int back[4]  = { BACKCOLOR , BACKCOLOR , BACKCOLOR , BACKCOLOR };
	if ( kbc_init(0) == 0 )
	{
		disable_irq(MOUSE_IRQ);
	}
	else mouse_sup = 1;
	enable_irq(KBD_IRQ);
	enable_irq(MOUSE_IRQ);
 	enable_irq(T0_IRQ);

#endif
	init_uart( base , 9600, 8, 1, PAR_NONE, true, true, true);
	//inicializar IER
	set_uart_register( base, SER_IER, RX_INT_EN | TX_INT_EN);
	load_char_table(table);
	clear_screen(BACKCOLOR, video_base);
	setup_peripherals();



	char op[5][32] = {"HANGMAN","Play Game","High Scores","Help","Exit"};
	char opcoes [5][32] = {"Play Game","One Player" , "Two Players - Server" ,"Two Players - Client" , "Go back" };
	Menu menu;
	initMenu( opcoes , &menu , 4);
	Menu menuPrincipal;
	initMenu( op , &menuPrincipal , 4);
	printMenu(&menuPrincipal , video_base, table);

	// Desenhar a forca e o enforcado no inicio do jogo 26
	drawBox(600, 250, 800, 500, FORECOLOR, 1, video_base );
	draw_gallow(750, 300, BACKCOLOR, video_base);
	draw_Hangman ( 750 , 300, BACKCOLOR, video_base , 6 );

	Player ** bestPlayers = (Player ** )malloc ( 11 * sizeof(Player * ) );
	int i = 0;
	for ( i = 0; i < 11; ++i)
		bestPlayers[i] = NULL;
	int last_sc = 0, option = 0;
	do
	{
#ifdef EMBED
	  if (hasKey())
	    scancode = GetScanCode();
#endif
        option = 0;
	#ifndef EMBED
		if ( mouse_sup != 0 )
		{
			if ( pack_completo )
			{
				parse_mouse_event(mouse);
				pack_completo = 0;
			}
			drawMouse( back , video_base );
			if ( ( option = evaluateMouse( &menuPrincipal ) ) != 0 )
				printMenu(&menuPrincipal, video_base, table);				
		}
	#endif
		if(scancode != last_sc || option == 2)
		{
			last_sc = scancode;
			if (scancode == SC_RET || option == 2)
			{
				option = selectedOption(&menuPrincipal);
				switch(option)
				{
					case 0 :
					#ifndef EMBED
						reinstall_asm_irq_handler(KBD_IRQ , &old_kbd_irq);
					#endif
						sub_menu(&menu , video_base, table , base ,  bestPlayers , mouse_sup );
						scancode = 0;
						last_sc = 0;
						clear_screen(BACKCOLOR, video_base);
						printMenu(&menuPrincipal, video_base, table);
						break;

					case 1 :
					#ifndef EMBED
						reinstall_asm_irq_handler(KBD_IRQ , &old_kbd_irq);
					#endif
						clear_screen(BACKCOLOR, video_base);
						print_best_scores(bestPlayers, video_base, table);
						scancode = 0;
						last_sc= 0;
						clear_screen(BACKCOLOR, video_base);
						printMenu(&menuPrincipal , video_base, table);
					#ifndef EMBED
						install_asm_irq_handler(KBD_IRQ, kbd_isr, &old_kbd_irq);
					#endif
						break;

					case 2 :
							#ifndef EMBED
								reinstall_asm_irq_handler(KBD_IRQ , &old_kbd_irq);
							#endif
							clear_screen(BACKCOLOR, video_base);
						     showManual( video_base, table );
							 clear_screen(BACKCOLOR, video_base);
						     printMenu(&menuPrincipal , video_base, table);
							#ifndef EMBED
								install_asm_irq_handler(KBD_IRQ, kbd_isr, &old_kbd_irq);
							#endif
							 break;

					case 3 : last_sc = 1;
						     break;
				}
				drawBox(600, 250, 800, 500, FORECOLOR, 1, video_base );
				draw_gallow(750, 300, BACKCOLOR, video_base);
				draw_Hangman ( 750 , 300, BACKCOLOR, video_base , 6 );
				option = 0;
			}
			if ( updateMenu( last_sc , &menuPrincipal) )
				printMenu(&menuPrincipal , video_base, table);
		}
	} while (last_sc != 1);

	clear_screen(BACKCOLOR, video_base);
	clear_screen(0, video_base);
	printString("It is working!!\n");
	printString("We are not using the text mode anymore because it is very ugly!! =)\n");
	printString("And because it would be too easy for us...\n");

#ifdef EMBED
	outportb(0x64, 0xFE);
#else
	disable_irq(KBD_IRQ);
	disable_irq(MOUSE_IRQ);
	disable_irq(T0_IRQ);
	set_uart_register(base, SER_IER, 0);
	reinstall_asm_irq_handler(KBD_IRQ , &old_kbd_irq);
	reinstall_asm_irq_handler(T0_IRQ , &old_timer_irq);
	//reinstall_asm_irq_handler(MOUSE_IRQ , &old_mouse_irq);
	enable_irq(KBD_IRQ);
//	enable_irq(MOUSE_IRQ);
	enable_irq(T0_IRQ);
	return 0;
#endif
}


